package cn.wolfcode.student.controller;


import cn.wolfcode.student.entity.SendMail;
import cn.wolfcode.student.entity.Student;
import cn.wolfcode.student.repository.StudentRepository;
import cn.wolfcode.student.service.IStudentService;
import cn.wolfcode.student.util.jsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")


public class StudentController {

    @Autowired(required = false)
    private SendMail sendMail;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    private IStudentService studentService;




    @RequestMapping("/save")

    public Object save(Student student){
        studentService.save(student);
        studentRepository.save(student);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("student"+student.getUsername(),student);

        return new jsonResult();

    }
    @RequestMapping("/aaaa")

    public Object aaaa(Student student){




        Student byName = studentService.getByName(student.getUsername());
        if (byName!=null){
            SendMail sendMails=new SendMail();
            sendMails.setNr(student);
            sendMails.setAddress("491217097@qq.com","2393430607@qq.com","hello");
            sendMails.setAffix(null,null);
            sendMails.send("smtp.qq.com","491217097@qq.com","sptbkjwqfepqcbde");

            return new jsonResult();
        }
     return new jsonResult(false,"错误");

    }

    @RequestMapping("/list")

    public Object aaa(){

        return studentService.list();

    }
}
